﻿using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using YetAnotherChat.TestWeb.DI;

namespace YetAnotherChat.TestWeb
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            RegisterDIContainer();
        }

        private void RegisterDIContainer()
        {
            // for usage of UnityContainer, please see unittest
            var container = new UnityContainer();
            // the problem with this approach is we have to type cast to get the real DependencyResolverImpl
            // and expose the container by some way to get the container's full functionality
            // which DependencyResolver.Current.GetService only provide basic function. Or some wrapper type/params need to
            // be implemented - wait for more...
            // DependencyResolver.SetResolver(new DependencyResolverImpl(container));
            // this DependencyResolver.Current.GetService will call DependencyResolverImpl.GetService

            // another approach from http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvcdependencyinjection_topic2.aspx
            //var factory = new UnityControllerFactory(container);
            //ControllerBuilder.Current.SetControllerFactory(factory);
        }
    }
}